package com.hgsf.controller;

import com.hgsf.entity.Teacher;
import com.hgsf.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping("/teacher")
@Controller
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private HttpServletRequest request;

    @ResponseBody
    @RequestMapping("/login")
    public String login(Teacher teacher,String checkcode){
        Teacher t = null;
        String s = "";
        HttpSession session = request.getSession();
        if (session.getAttribute("checkcode").equals(checkcode)) {

            try {
                t = teacherService.teacherLogin(teacher);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (t != null) {
                s="success";
                session.setAttribute("status","teacher");
                session.setAttribute("name",t.getTname());
            } else {
                session.setAttribute("status","psw_wrong");
                s="failed";
            }
        }
        else {
            session.setAttribute("status","verify_wrong");
            s="failed";
        }
        return s;
    }
}
